package net.toby200.canalbuddy.notices.map;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import net.toby200.canalbuddy.R;
import net.toby200.canalbuddy.notices.Event;
import net.toby200.canalbuddy.notices.NoticeFilter;
import net.toby200.canalbuddy.notices.NoticesManager;
import net.toby200.canalbuddy.notices.model.Location;
import net.toby200.canalbuddy.notices.model.Notice;

import java.util.Collection;
import java.util.logging.Logger;

import de.greenrobot.event.EventBus;

/**
 * Created by toby on 17/07/2014.
 */
public class CanalMapFragment extends MapFragment  {
    private static final Logger LOG = Logger.getLogger(CanalMapFragment.class.getName());

    static final LatLng HOME = new LatLng(51.732, -1.3256);
    static final float DEFAULT_ZOOM = 8;



    public static CanalMapFragment newInstance() {
        return new CanalMapFragment();
    }

    @Override
    public void onAttach(Activity activity) {
        LOG.info("onAttach");
        super.onAttach(activity);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LOG.info("onCreateView");
        View view = super.onCreateView(inflater, container, savedInstanceState);

        GoogleMap map = getMap();

        final NoticesManager noticeManager = (NoticesManager) getFragmentManager().findFragmentByTag(
                getString(R.string.notice_manager_frag));

        map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                Notice notice = noticeManager.getNotice(marker.getTitle());
                EventBus.getDefault().post(new Event.NoticeSelectedEvent(notice));
            }
        });

        map.setMyLocationEnabled(true);
        CameraUpdate homeCam = CameraUpdateFactory.newLatLngZoom(HOME, DEFAULT_ZOOM);
        map.moveCamera(homeCam);

        onNoticeUpdate(noticeManager.getNotices());

        return view;
    }

    public void onNoticeUpdate(Collection<Notice> notices) {
        Collection<Notice> filteredNotices = new NoticeFilter(getActivity().getApplicationContext()).getFilteredNotices(notices);
        addNotices(getMap(), filteredNotices);
    }

    private void addNotices(GoogleMap map, Collection<Notice> notices) {
        LOG.info("addNotices");
        if (map != null) {
            map.clear();
            for (Notice notice : notices) {
                MarkerOptions marker = getMarker(notice);
                if (marker != null) {
                    map.addMarker(marker);
                }
            }
        } else {
            LOG.warning("No map ready yet... unable to add notices");
        }
    }

    private MarkerOptions getMarker(Notice notice) {
        LatLng pos = getPosition(notice.getStartLoc());

        return pos != null ?  new MarkerOptions()
                .position(pos)
                .title(notice.getTitle())
                .icon(getIcon(notice.getType()))
                : null;

    }

    private BitmapDescriptor getIcon(String type) {
        float hue = BitmapDescriptorFactory.HUE_BLUE;

        if (type.contains("estriction")) {
            hue = BitmapDescriptorFactory.HUE_RED;
        } else if (type.contains("dvice")) {
            hue = BitmapDescriptorFactory.HUE_CYAN;
        }  else if (type.contains("Opening Times")) {
            hue = BitmapDescriptorFactory.HUE_GREEN;
        } else if (type.contains("Towpath")) {
            hue = BitmapDescriptorFactory.HUE_ORANGE;
        }

        return BitmapDescriptorFactory.defaultMarker(hue);
    }

    private LatLng getPosition(Location loc) {
        return loc != null ? new LatLng(loc.getLat(), loc.getLng()) : null;
    }
}